<?php

/*
 * @File: ResizeClassImage_helper.php
 * @Date: 16-05-2013
 * @Developer: kln teams
 * @powered by KLN
 */

class ResizeClassImage {
	var $test = 12;
	var $image;
	var $image_type;
    //================================================
    /**
    * @method checkType
    * @param  $filetype
    * @return true or false 
    * @powered by Khmer Loving News
    */
    
	public function checkType($filetype) {
		$type = substr($filetype, 0, 5);
		if ($type == 'image') {
			return true;
		} else {
			return false;
		}
	}
	//==================================================
	/**
    * @method checkSize
    * @param  $filesize
    * @return true or false
    * @powered by Khmer Loving News
    */
    
	public function checkSize ($filesize) {
		if ( $filesize < (1024 * 1024) ) {
			return true;
		} else {
			return false;
		}
	}
    
    //==================================================
    /**
    * @method load
    * @param  $filename
    * @return image type 
    * @powered by Khmer Loving News
    */
	public function load($filename) {

		$image_info = getimagesize($filename);
		$this->image_type = $image_info[2];
		if ($this->image_type == IMAGETYPE_JPEG) {

			$this->image = imagecreatefromjpeg($filename);
		} elseif ($this->image_type == IMAGETYPE_GIF) {

			$this->image = imagecreatefromgif($filename);
		} elseif ($this->image_type == IMAGETYPE_PNG) {

			$this->image = imagecreatefrompng($filename);
		}
	}
    //===================================================
    /**
    * @method save
    * @param  $filename
    * @param  $image_type
    * @param  $compression
    * @param  $permissions
    * @return string 
    * @powered by Khmer Loving News
    */
	public function save($filename, $image_type = IMAGETYPE_JPEG, $compression = 75, $permissions = null) {

		if ($image_type == IMAGETYPE_JPEG) {
			imagejpeg($this->image, $filename, $compression);
		} elseif ($image_type == IMAGETYPE_GIF) {

			imagegif($this->image, $filename);
		} elseif ($image_type == IMAGETYPE_PNG) {

			imagepng($this->image, $filename);
			
		}
		if ($permissions != null) {

			chmod($filename, $permissions);
		}
	}
    //==============================================================
    /**
    * @method output
    * @param $image_type
    * @return string
    * @powered by Khmer Loving News
    */
	public function output($image_type = IMAGETYPE_JPEG) {

		if ($image_type == IMAGETYPE_JPEG) {
			imagejpeg($this->image);
		} elseif ($image_type == IMAGETYPE_GIF) {

			imagegif($this->image);
		} elseif ($image_type == IMAGETYPE_PNG) {

			imagepng($this->image);
		}
	}
    //======================================================
    /**
    * @method getWidth
    * @return string
    * @powered by Khmer Loving News
    */
	public function getWidth() {

		return imagesx($this->image);
	}
    //======================================================
    /**
    * @method getHeight
    * @return string
    * @powered by Khmer Loving News
    */
	public function getHeight() {

		return imagesy($this->image);
	}
    //======================================================
    /**
    * @method resizeToHeight
    * @return string
    * @param $height
    * @powered by Khmer Loving News
    */
	public function resizeToHeight($height) {
		$ratio = $height / $this->getHeight();
		$width = $this->getWidth() * $ratio;
		$this->resize($width, $height);
	}
    //======================================================
    /**
    * @method resizeToWidth
    * @return string
    * @param $width
    * @powered by Khmer Loving News
    */
	public function resizeToWidth($width) {
		$ratio = $width / $this->getWidth();
		$height = $this->getheight() * $ratio;
		$this->resize($width, $height);
	}
    //======================================================
    /**
    * @method scale
    * @return string
    * @param $scale
    * @powered by Khmer Loving News
    */
	public function scale($scale) {
		$width = $this->getWidth() * $scale / 100;
		$height = $this->getheight() * $scale / 100;
		$this->resize($width, $height);
	}
    //======================================================
    /**
    * @method resize
    * @return string
    * @param $width
    * @param $height
    * @powered by Khmer Loving News
    */
	public function resize($width, $height) {
		$new_image = imagecreatetruecolor($width, $height);
		imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
		$this->image = $new_image;
	}
    //======================================================
    /**
    * @method random
    * @return string
    * @param $length
    * @powered by Khmer Loving News
    */
	public function random($length) {
		$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
		$size = strlen($chars);
		$str = '';
		for ($i = 0; $i < $length; $i++) {
			$str .= $chars[rand(0, $size - 1)];
		}
		return $str;
	}
	
}

?>